package cn.su.upload_download.controller;

import cn.su.upload_download.entity.DownloadParam;
import cn.su.upload_download.entity.MyFile;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SuZuQi
 * @title: DownLoadController
 * @projectName upload_download
 * @description: TODO
 * @date 2019/6/28
 */
@Controller
public class DownLoadController {

    @GetMapping("downLoad")
    @ResponseBody
    public List<MyFile> getLoad() {
        return getAllFiles("D:\\workspace_0425\\front\\public\\static\\js");
    }

    @PostMapping("downloadFile")
    public ResponseEntity downloadFile(HttpServletResponse response, @RequestBody DownloadParam downloadParam) throws Exception {
        //
        File sourceFile = new File(downloadParam.getDownloadUrl());
        if(!sourceFile.exists())  {
            PrintWriter out = response.getWriter();
            out.write("文件已丢失");
            out.close();
            return null;
        }
        String foldName = URLEncoder.encode(sourceFile.getName(),
                "utf-8");
        // 做下载
        //将下载的文件，封装byte[]
        byte[] bytes=null;
        try {
            bytes = FileUtils.readFileToByteArray(sourceFile);//把硬盘上要下载的文件转化成bytes类的数组
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建封装响应头信息的对象
        HttpHeaders header=new HttpHeaders();
        //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
        header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //设置下载的文件的名称
        header.setContentDispositionFormData("attachment", foldName);
        //创建ResponseEntity对象
        ResponseEntity entity=
                new ResponseEntity(bytes, header, HttpStatus.CREATED);
        return entity;
    }


    private List<MyFile> getAllFiles(String rootPath) {
        File rootFile = new File(rootPath);
        List<MyFile> fileList = new ArrayList<>();
        String filePath = rootFile.getAbsolutePath();
        filePath = filePath.replaceAll("\\\\ ", "/");
        Boolean isDirectory = false;
        String fileName = rootFile.getName();
        List<MyFile> childFiles = new ArrayList<>();
        if (isDirectory = rootFile.isDirectory()) {
            File[] files = rootFile.listFiles();
            for (File file : files) {
                childFiles.addAll(getAllFiles(file.getAbsolutePath()));
            }
        }
        MyFile myFile = new MyFile(fileName, filePath, isDirectory, childFiles);
        List<MyFile> list = new ArrayList<>();
        list.add(myFile);
        return list;


    }
}
